
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <uv.h>
#define ASSERT(expr)                                      \
 do {                                                     \
  if (!(expr)) {                                          \
    fprintf(stderr,                                       \
            "Assertion failed in %s on line %d: %s\n",    \
            __FILE__,                                     \
            __LINE__,                                     \
            #expr);                                       \
    abort();                                              \
      }                                                       \
   } while (0)

#define TEST_PORT 6080

uv_loop_t loop;
uv_tcp_t tcp_client;
uv_connect_t connection_request;
uv_write_t write_request;


static void write_cb(uv_write_t *req, int status) {
	//ASSERT(status == 0);//UV_ECANCELED
	UV_ECANCELED;
	printf("write_cb status: %d, %s\n", status, strerror(status));
	uv_close((uv_handle_t*)req->handle, NULL);
}


static void connect_cb(uv_connect_t *req, int status) {
	uv_buf_t buf = uv_buf_init("HELLO", 5);
	//ASSERT(status == 0);
	printf("connect_cb status: %d\n", status);
	int r = uv_write(&write_request,
		(uv_stream_t *)&tcp_client,
		&buf, 1,
		write_cb);
	int a = 2;
}

int main(int argc, char** argv)
{
	memset(&loop, 0, sizeof(uv_loop_t));
	memset(&tcp_client, 0, sizeof(uv_tcp_t));
	memset(&connection_request, 0, sizeof(uv_connect_t));
	struct sockaddr_in sa;
	ASSERT(0 == uv_ip4_addr("192.168.26.220", 9012, &sa));
	ASSERT(0 == uv_loop_init(&loop));
	ASSERT(0 == uv_tcp_init(&loop, &tcp_client));

	ASSERT(0 == uv_tcp_connect(&connection_request,
		&tcp_client,
		(const struct sockaddr *)
		&sa,
		connect_cb));
	uv_run(&loop, UV_RUN_DEFAULT);
	return 0;
}